import { Notify } from 'vant';
import store from "../store/index"
import storage from "./storage"

/**
 * 封装 websocket
 * @param Vue
 * @param options
 */
export default {
    install(Vue) {
        let socket = null
        let wss_url = "ws://localhost:8081/api/user/ws/"
        let setIntervalWebsocketPing = null
        Vue.prototype.ws = {
            /**
             * 建立连接
             */
            createWs() {
                var vuexWs = store.getters.getWsStatus //获取vuex中ws状态
                var token = localStorage.getItem("token")
                if (token != null && !vuexWs) {
                    socket = new WebSocket(wss_url + token)
                    socket.onopen = (e) => this.onOpenWs(e)
                    socket.onerror = (e) => this.onErrorWs(e)
                    socket.onclose = (e) => this.onCloseWs(e)
                    socket.onmessage = (e) => this.onMessageWs(e)
                    clearInterval(setIntervalWebsocketPing)
                }
            },

            /**
             * 连接成功以后
             */
            onOpenWs() {
                var fr = storage.getString("oneConn")
                if (fr != null) {
                    Notify({ type: 'success', message: '连接成功' });
                    storage.remove("oneConn")
                }
                this.sendPing()
            },

            /**
             * 连接错误
             */
            onErrorWs() {
                Notify({ type: 'warning', message: '连接错误' });
                clearInterval(setIntervalWebsocketPing)
            },

            /**
             * 接收数据
             * @param e
             */
            onMessageWs(e) {
                console.log(e)
                var msg = JSON.parse(e.data)
                var f = [...storage.getObj("friends")]//获取好友的信息
                console.log("f", f)
                console.log("msg", msg)
                f.forEach((e, k) => {
                    if (e.id == msg.userid) {
                        msg.key = k
                        msg.send = 1
                        store.dispatch("push", msg)
                        return
                    }
                });
            },

            /**
             * 发送数据
             * @param e
             */
            onSendWs(e) {
                socket.send(JSON.stringify(e))
            },

            /**
             * 断开
             */
            onCloseWs() {
                Notify({ type: 'warning', message: '连接断开' });
                socket.close()
                clearInterval(setIntervalWebsocketPing)
            },

            /**
             * 发送心跳
             */
            sendPing() {
                var ping = {
                    id: new Date().getTime(),
                    userid: 0,
                    cmd: 3,//3代表是心跳
                    dstid: 0,
                    dstimg: "",
                    content: "",
                }
                this.onSendWs(ping)
                setIntervalWebsocketPing = setInterval(() => {
                    this.onSendWs(ping)
                }, 5000)
            }
        }
    }
}






